import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:tencent_cos/tencent_cos.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  List<Asset> images = List<Asset>();
  String _error;
  String path;

  @override
  void initState() {
    super.initState();
    //initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await TencentCos.platformVersion;
      final int ret = await TencentCos.initCosServer("1301206366", "jianli", "ap-guangzhou",
          "http://111.231.235.92:10006/app/thirdpartSdk/getCosCredential",
          "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDcyNTAsImFjY291bnQiOiIxNTUwMDAwMDAwNSIsInVzZXJfbmFtZSI6Ilfnu4TlhoXkuJPnm5EiLCJwaG9uZSI6IjE1NTAwMDAwMDA1Iiwid29rZXIiOiIiLCJtYWlsIjoiIiwic3RhdGUiOjAsImxlYWRlcl9pZCI6MCwiZGVwYXJ0bWVudF9pZCI6MCwicm9sZV9pZCI6MCwibGFzdF9sb2dpbl9pcCI6IiIsImNvbXBhbnlfaWQiOiIiLCJsYXN0X2xvZ2luX3RpbWUiOjAsImRhdGFfcGVybWlzc2lvbnMiOiIiLCJsb2dpbl90eXBlIjoxLCJpYXQiOjE1ODk0NDg0NjN9.7yxmodFYTMtQvMvekV86CXqxFKO-W9dNtR7D3BWecZQ");
      print(ret);
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<void> loadAssets() async {
//    setState(() {
//      images = List<Asset>();
//    });
//
//    List<Asset> resultList;
//    String error;
//
//    try {
//      resultList = await MultiImagePicker.pickImages(
//        maxImages: 300,
//      );
//    } on Exception catch (e) {
//      error = e.toString();
//    }
//
//    // If the widget was removed from the tree while the asynchronous platform
//    // message was in flight, we want to discard the reply rather than calling
//    // setState to update our non-existent appearance.
//    if (!mounted) return;
//
//    setState(() {
//      images = resultList;
//      if (error == null) _error = 'No Error Dectected';
//    });
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    path = image.path;
    print(image.path);
  }

  Future uploadAssets() async {
    try{
      var rsp = await TencentCos.upload(path, "sltest/");
      print(rsp);
    }catch(e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
            child: ListView(
              children: <Widget>[
                Text('Running on: $_platformVersion\n'),
                RaisedButton(
                  onPressed: (){
                    initPlatformState();
                  },
                  child: Text("初始化", style: TextStyle(fontSize: 14, color: Colors.blue),),
                ),
                RaisedButton(
                  onPressed: loadAssets,
                  child: Text("图片选择", style: TextStyle(fontSize: 14, color: Colors.blue),),
                ),
                RaisedButton(
                  onPressed: uploadAssets,
                  child: Text("图片上传", style: TextStyle(fontSize: 14, color: Colors.blue),),
                ),
              ],
            )
        ),
      ),
    );
  }
}
